package com.ssm.controller;

import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.ssm.pojo.BookInfo;
import com.ssm.service.BookInfoService;
@Controller
@RequestMapping("/book")
public class BookInfoController {
	@Resource(name="bookInfoService")
	private BookInfoService bookInfoService;
	
	@RequestMapping(value="/all",method=RequestMethod.GET)
	public String all(HttpServletRequest request,ModelMap modelMap){
		String bookName = request.getParameter("bookName");
		Integer from=0;
		if(request.getParameter("from")!=null){
			from =Integer.valueOf(request.getParameter("from"));
		}
		Integer pageSize = 5;
		if(request.getParameter("pageSize")!=null){
			pageSize =Integer.valueOf(request.getParameter("pageSize"));
		}
		List<BookInfo> booklist = bookInfoService.queryList(bookName, pageSize, from);
		modelMap.addAttribute("bookList", booklist);
		return "/list";
	}
	@RequestMapping(value="/add",method=RequestMethod.POST)
	public void getAdd(HttpServletRequest req,BookInfo bookInfo,HttpServletResponse resp) throws IOException, ParseException{
		System.out.println("a============");
		req.setCharacterEncoding("utf-8");
		
		
		/*BookInfo bookInfo=new BookInfo();
		bookInfo.setBookCode(req.getParameter("bookCode"));
		bookInfo.setBookType(Integer.parseInt(req.getParameter("bookType")));
		bookInfo.setBookName(req.getParameter("bookName"));
		bookInfo.setBookAuthor(req.getParameter("bookAuthor"));
		bookInfo.setBorrowed(Integer.parseInt(req.getParameter("borrowed")));
		bookInfo.setCreatedBy(req.getParameter("createdBy"));
		bookInfo.setCreationTime(req.getParameter("creationTime"));
		bookInfo.setLastUpdatetime(req.getParameter("lastUpdatetime"));
		bookInfo.setUblishDate(req.getParameter("ublishDate"));
		bookInfo.setPublishPress(req.getParameter("publishPress"));*/
		int sa=bookInfoService.addBookInfo(bookInfo);
		if(sa>0){
			System.out.println("a==ssssss==========");
			req.setAttribute("sa", sa);
			resp.getWriter().print("<script> alert('ok');"+"location.href='http://localhost:8080//BookManager/book/all' </script>");
		}else{
			resp.getWriter().print("<script> alert('ok');"+"location.href='http://localhost:8080//BookManager/book/all' </script>");
		}
		}
	
	@RequestMapping(value="/one")
	public String querybyid(HttpServletRequest request,ModelMap modelMap,Integer bookId){
		//Integer bookId = Integer.parseInt(request.getParameter("bookId"));
		
		List<BookInfo> bookList = bookInfoService.queryListById(bookId);
		request.setAttribute("bookList", bookList);
		//modelMap.addAttribute("bookList", bookList);
		return "/one";
	}
	@RequestMapping(value="/delete")
	public void deleteBookinfo(HttpServletRequest req,HttpServletResponse resp,Integer bookId) throws IOException{
		//Integer bookId = Integer.parseInt(request.getParameter("bookId"));
		int num = bookInfoService.deleteBookInfo(bookId);
		if(num>0){
			req.setAttribute("num", num);
			resp.getWriter().print("<script> alert('ok');"+"location.href='http://localhost:8080//BookManager/book/all' </script>");
		}else{
			resp.getWriter().print("<script> alert('ok');"+"location.href='http://localhost:8080//BookManager/book/all' </script>");
		}
		
	}
	@RequestMapping(value="/update")
	public String updatebyid(HttpServletRequest request,ModelMap modelMap,Integer bookId){
		//Integer bookId = Integer.parseInt(request.getParameter("bookId"));
		
		List<BookInfo> bookList = bookInfoService.queryListById(bookId);
		request.setAttribute("bookList", bookList);
		//modelMap.addAttribute("bookList", bookList);
		return "/update";
	}
	@RequestMapping(value="/update1",method=RequestMethod.POST)
	public void getupdate(HttpServletRequest req,HttpServletResponse resp) throws IOException, ParseException{
		System.out.println("a============");
		req.setCharacterEncoding("utf-8") ;
		BookInfo bookInfo=new BookInfo();
		System.out.println(req.getParameter("bookId"));
		bookInfo.setBookId(Integer.parseInt(req.getParameter("bookId")));
		bookInfo.setBookCode(req.getParameter("bookCode"));
		bookInfo.setBookType(Integer.parseInt(req.getParameter("bookType")));
		bookInfo.setBookName(req.getParameter("bookName"));
		bookInfo.setBookAuthor(req.getParameter("bookAuthor"));
		bookInfo.setBorrowed(Integer.parseInt(req.getParameter("borrowed")));
		bookInfo.setCreatedBy(req.getParameter("createdBy"));
		bookInfo.setCreationTime(req.getParameter("creationTime"));
		bookInfo.setLastUpdatetime(req.getParameter("lastUpdatetime"));
		bookInfo.setUblishDate(req.getParameter("ublishDate"));
		bookInfo.setPublishPress(req.getParameter("publishPress"));
		int sa=bookInfoService.updateBookInfo(bookInfo);
		if(sa>0){
			System.out.println("a==ssssss==========");
			req.setAttribute("sa", sa);
			resp.getWriter().print("<script> alert('ok');"+"location.href='http://localhost:8080//BookManager/book/all' </script>");
		}else{
			resp.getWriter().print("<script> alert('ok');"+"location.href='http://localhost:8080//BookManager/book/all' </script>");
		}
		}
}
